package br.com.as.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import br.com.as.dominio.Compra;
import br.com.as.dominio.Fornecedor;
import br.com.as.dominio.ItemCompra;
import br.com.as.dominio.Produto;
import br.com.as.utils.HibernateUtility;

/**
 * Classe para consistencia de compra
 * 
 * @author msante
 *
 */
public class CompraDAO {

	@SuppressWarnings("unchecked")
	public  List<Compra> listar() throws Exception{
		List<Compra> lista = null;
		Session sessao = HibernateUtility.getSession();
		try {
			Query query = sessao.createQuery(" from Compra ");		
			lista = query.list();

			for (Compra compra : lista) {
				Query query2 = sessao.createQuery(" from ItemCompra i where i.compra.id =:id ");
				query2.setLong("id", compra.getId());
				compra.setItensCompra(query2.list());
			}
			
		} catch (Exception e) {			
			throw e;
		} finally {
			sessao.close();
		}
		return lista;
	}

	public  void incluir(Compra compra) throws Exception{
		Transaction tx = null;
		Session sessao = HibernateUtility.getSession();
		try {			
			tx = sessao.beginTransaction();			
			sessao.saveOrUpdate(compra);
			for (ItemCompra item : compra.getItensCompra()) {
				item.setCompra(compra);
				sessao.saveOrUpdate(item);				
			}
			tx.commit();
		} catch (Exception e) {
			tx.rollback();
			sessao.close();
			throw e;
		}finally{
			sessao.close();	
		}
	}
	
	public  Compra consultar(Long notaFiscal, Fornecedor fornecedor) throws Exception{
		Compra compra = null;
		Session sessao = HibernateUtility.getSession();
		try {
			Query query = sessao.createQuery("from Compra cli where cli.notaFiscal =:notaFiscal and cli.fornecedor.id =:id ");		
			query.setLong("notaFiscal", notaFiscal);
			query.setLong("id", fornecedor.getId());
			compra = (Compra) query.uniqueResult();			
		} catch (Exception e) {			
			throw e;
		} finally {
			sessao.close();
		}
		return compra;		
	}
	
	@SuppressWarnings("unchecked")
	public List<Compra> consultar(Fornecedor fornecedor) throws Exception{
		Session sessao = HibernateUtility.getSession();
		try {
			Query query = sessao.createQuery("from Compra cli where cli.fornecedor.id =:idFornecedor");		
			query.setLong("idFornecedor", fornecedor.getId());
			return query.list();			
		} catch (Exception e) {			
			throw e;
		} finally {
			sessao.close();
		}
	}

	@SuppressWarnings("unchecked")
	public List<ItemCompra> consultar(Produto produto) throws Exception{
		Session sessao = HibernateUtility.getSession();
		try {
			Query query = sessao.createQuery("from ItemCompra cli where cli.produto.id =:id");		
			query.setLong("id", produto.getId());
			return query.list();			
		} catch (Exception e) {			
			throw e;
		} finally {
			sessao.close();
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<ItemCompra> consultar(Compra compra) throws Exception{
		Session sessao = HibernateUtility.getSession();
		try {
			Query query = sessao.createQuery("from ItemCompra cli where cli.compra.id =:id");		
			query.setLong("id", compra.getId());
			return query.list();			
		} catch (Exception e) {			
			throw e;
		} finally {
			sessao.close();
		}
	}
	
	public void excluir(Compra compra) throws Exception{
		Transaction tx = null;
		Session sessao = HibernateUtility.getSession();
		try {			
			tx = sessao.beginTransaction();
			for (ItemCompra it : compra.getItensCompra()) {
				sessao.delete(it);
			}
			sessao.delete(compra);
			tx.commit();
		} catch (Exception e) {
			tx.rollback();
			sessao.close();
			throw e;
		}finally{
			sessao.close();	
		}
	}
	
	public void alterar(Compra compra, List<ItemCompra> itensExcluir, List<ItemCompra> itensAlterados, List<ItemCompra> itensNovos) throws Exception{
		Transaction tx = null;
		Session sessao = HibernateUtility.getSession();
		try {			
			tx = sessao.beginTransaction();
			sessao.saveOrUpdate(compra.getFornecedor());
			
			for (ItemCompra it : itensNovos) {
				sessao.save(it);
			}
			
			for (ItemCompra it : itensAlterados) {
				sessao.update(it);
			}
			
			for (ItemCompra it : itensExcluir) {
				sessao.delete(it);
			}
			
			sessao.update(compra);
			tx.commit();
		} catch (Exception e) {
			tx.rollback();
			sessao.close();
			throw e;
		}finally{
			sessao.close();	
		}
	}

}
